package com.zk.util;

import java.sql.Timestamp;
import java.text.*;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 时间转换工具类
 * @author bairuqiang
 * @date 2016年3月31日 上午11:10:32
 */
public class DateUtil {
	
	public static final String TIME_FORMATTER_BASIC = "yyyy-MM-dd HH:mm:ss";
	public static final String TIME_FORMATTER_SHORT = "yyyyMMddHHmmss";
	public static final String TIME_FORMATTER_SHORT_MINI = "yyyyMMddHHmmssSSS";
	public static final String DATE_FORMATTER_SHORT = "yyyyMMdd";
	public static final String DATE_FORMATTER_NORMAL = "yyyy-MM-dd";
	public static final String DATE_FORMATTER_START = " 00:00:00";
	public static final String DATE_FORMATTER_END = " 23:59:59";
	
	/**
	 * Timestamp -> String
	 * @param ts
	 * @return
	 */
	public static String getStringDateFromTimestamp(Timestamp ts) {
		String tsStr = "";
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			//方法一
			tsStr = sdf.format(ts);
			return tsStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Timestamp -> String
	 * @param ts
	 * @return
	 */
	public static String getStringDateFromTimestamp2(Timestamp ts) {
		String tsStr = "";
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			//方法一
			tsStr = sdf.format(ts);
			return tsStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Timestamp -> String
	 * @param ts
	 * @param formatter
	 * @return
	 */
	public static String getStringDateFromTimestamp(Timestamp ts, String formatter) {
		String tsStr = "";
		DateFormat sdf = new SimpleDateFormat(formatter);
		try {
			if (ts == null) {
				return null;
			}
			tsStr = sdf.format(ts);
			return tsStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * String -> Timestamp
	 * @param str
	 * @param formatter
	 * @return
	 */
	public static Timestamp getTimestampDateFromString(String str, String formatter) 
	{
		SimpleDateFormat format = new SimpleDateFormat(formatter);
		format.setLenient(false);
		Timestamp date = null;
		try {
			if (null != str)
			{
				date = new Timestamp(format.parse(str).getTime());
				return date;
			}
		} catch (Exception e)
		{
			return null;
		}
		return date;
	}
	
	/**
	 * 用于验证格式
	 * @param str
	 * @param formatter
	 * @return
	 */
	public static Timestamp validateTimestampDateFromString(String str, String formatter) throws Exception
	{
		SimpleDateFormat format = new SimpleDateFormat(formatter);
		format.setLenient(false);
		Timestamp date = null;
		if (null != str)
		{
			date = new Timestamp(format.parse(str).getTime());
			return date;
		}
		return date;
	}
	
	/**
	 * 获得当前日期时间，类型为Timestamp
	 * @return
	 */
	public static Timestamp getCurrentDateTime() {
		Timestamp time = new Timestamp(System.currentTimeMillis()); 
		return time;
	}
    /**
     * 判断输入的字符串是否满足时间格式 
     * @param patternString 需要验证的字符串
     * @param type 验证的时间格式类型(1: yyyy-MM-dd HH:mm:ss、2：yyyy-MM-dd)
     * @return 合法返回 true ; 不合法返回false
     */
   public static boolean isTimeLegal(String patternString,int type) {
	     Pattern a=null;
	     if(type==1){
	    	    a=Pattern.compile("^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"); 
	     }else if(type==2){
	    		a= Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");	
	     }
         Matcher b=a.matcher(patternString); 
      if(b.matches()){
            return true;
      }else{
            return false;
      }
   }
   /**
    * 两个时间相差时间
    * @param startTime
    * @param endTime
    * endTime>startTime
    * @return 返回XX天
    */
   public static String getTwoTimeDay(String startTime, String endTime){
     	 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     	 long nday = 1000 * 24 * 60 * 60;
		try {
		    Date starDate = df.parse(startTime);
		    Date endDate=df.parse(endTime);
			// 获得两个时间的毫秒时间差异
			long diffTime=endDate.getTime()-starDate.getTime();
			//计算差多少天
            long day=diffTime/nday;
            return day+"";
		}catch (ParseException e) {			
			e.printStackTrace();
			return "";
		}       
   }
   /**
    * 两个时间相差毫秒
    * @param startTime
    * @param endTime
    * endTime>startTime
    * @return 返回XX毫秒
    */
   public static String getTwoTimeSec(String startTime, String endTime){
         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date starDate = df.parse(startTime);
            Date endDate=df.parse(endTime);
            // 获得两个时间的毫秒时间差异
            long diffTime=endDate.getTime()-starDate.getTime();
            return diffTime+"";
        }catch (ParseException e) {         
            e.printStackTrace();
            return "";
        }       
   }
   
   /**
    * 得到几个月后的时间
    * @param d
    * @param month
    * @return
    */
   public static Date getDateAfterMonth(Date d, int month) {
	   Calendar now = Calendar.getInstance();
	   now.setTime(d);
	   now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);
	   return now.getTime();
   }
   
   /** 
    * 得到几天前的时间 
    *  
    * @param d 
    * @param day 
    * @return 
    */  
   public static Date getDateBefore(Date d, int day) {  
       Calendar now = Calendar.getInstance();  
       now.setTime(d);  
       now.set(Calendar.DATE, now.get(Calendar.DATE) - day);  
       return now.getTime();  
   }  
 
   /** 
    * 得到几天后的时间 
    *  
    * @param d 
    * @param day 
    * @return 
    */  
   public static Date getDateAfter(Date d, int day) {  
       Calendar now = Calendar.getInstance();  
       now.setTime(d);  
       now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
       return now.getTime();  
   } 
   
   /**
    * 获得明天
    * @return
    */
   public static Timestamp getTomorrow(String formatter) {
	   Date ddd = DateUtil.getDateAfter(new Date(), 1);
	   DateFormat sdf = new SimpleDateFormat(formatter);  
	   String dateStr = sdf.format(ddd);
	   Timestamp startDate = DateUtil.getTimestampDateFromString(dateStr, formatter);
	   return startDate;
   }
   /***
    * 把字符串转成yyyy-MM-dd hh:mm:ss日期格式
    * @param strDate
    * @return
    */
   public static Date strToDate(String strDate) {
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	   ParsePosition pos = new ParsePosition(0);
	   Date strtodate = formatter.parse(strDate, pos);
	   return strtodate;
	  }
   /***
    * 把字符串转成yyyyMMdd日期格式
    * @param strDate
    * @return
    */
   public static Date str2Date(String strDate) {
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
	   ParsePosition pos = new ParsePosition(0);
	   Date strtodate = formatter.parse(strDate, pos);
	   return strtodate;
	  }
  /***
   * 把日期转成yyyyMMddHHmmss字符
   * @param time
   * @return
   */
   public static String getStringToDate(Date time) {
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
	   String dateString = formatter.format(time);
	   return dateString;
	}
   /***
    * 把日期转成yyyyMMdd字符
    * @param time
    * @return
    */
    public static String getString2Date(Date time) {
 	   SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
 	   String dateString = formatter.format(time);
 	   return dateString;
 	}

}
